package com.qianhui.dao;

import com.qianhui.common.IResult;
import com.qianhui.pojo.CommissionDetailRecord;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * CommissionDetailRecord entities. Transaction control of the save(), update()
 * and delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see com.qianhui.pojo.CommissionDetailRecord
 */
public class CommissionDetailRecordDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(CommissionDetailRecordDAO.class);
    // property constants
    public static final String BUYER_ID = "buyerId";
    public static final String ORDER_ID = "orderId";
    public static final String GOODS_ID = "goodsId";
    public static final String GOODS_NUMBER = "goodsNumber";
    public static final String BUYER_COMMISSION = "buyerCommission";

    public CommissionDetailRecord save(CommissionDetailRecord transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }

    public List<CommissionDetailRecord> saveList(List<CommissionDetailRecord> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (CommissionDetailRecord temp : list)
            {
                getSession().saveOrUpdate(temp);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return list;
    }

    public int delete(CommissionDetailRecord persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public CommissionDetailRecord findById(java.lang.String id)
    {
        log.debug("getting CommissionDetailRecord instance with id: " + id);
        try
        {
            CommissionDetailRecord instance = (CommissionDetailRecord) getSession().get("com.qianhui.pojo.CommissionDetailRecord", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding CommissionDetailRecord instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from CommissionDetailRecord as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (RuntimeException re)
        {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List findByBuyerId(Object buyerId)
    {
        return findByProperty(BUYER_ID, buyerId);
    }

    public List findByOrderId(Object orderId)
    {
        return findByProperty(ORDER_ID, orderId);
    }

    public List findByGoodsId(Object goodsId)
    {
        return findByProperty(GOODS_ID, goodsId);
    }

    public List findByGoodsNumber(Object goodsNumber)
    {
        return findByProperty(GOODS_NUMBER, goodsNumber);
    }

    public List findByBuyerCommission(Object buyerCommission)
    {
        return findByProperty(BUYER_COMMISSION, buyerCommission);
    }

    public CommissionDetailRecord findByBuyerIdAndOrderIdAndGoodsId(String buyerId, String orderId, String goodsId)
    {
        try
        {
            String sql = "from CommissionDetailRecord c where c.buyerId=:buyerId and c.orderId=:orderId and c.goodsId=:goodsId";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("orderId", orderId);
            query.setParameter("goodsId", goodsId);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return (CommissionDetailRecord) tempList.get(0);
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalGoodsSalesNumberByBuyerIdAndGoodsIdAndBeginTimeAndEndTime(String buyerId, String goodsId, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select sum(c.goodsNumber) from CommissionDetailRecord c where c.buyerId=:buyerId and c.goodsId=:goodsId and c.recordTime between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("goodsId", goodsId);
            query.setDate("beginTime", beginTime);
            query.setDate("endTime", endTime);

            List li = query.list();
            if (li == null || li.isEmpty())
            {
                return 0;
            }
            else if (li.get(0) == null)
            {
                return 0;
            }
            else
            {
                return ((Number) li.get(0)).intValue();
            }

        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public double getTotalCommissionByBuyerIdAndGoodsIdAndBeginTimeAndEndTime(String buyerId, String goodsId, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select sum(c.buyerCommission) from CommissionDetailRecord c where c.buyerId=:buyerId and c.goodsId=:goodsId and c.recordTime between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("goodsId", goodsId);
            query.setDate("beginTime", beginTime);
            query.setDate("endTime", endTime);

            List li = query.list();
            if (li == null || li.isEmpty())
            {
                return 0;
            }
            else if (li.get(0) == null)
            {
                return 0;
            }
            else
            {
                return ((Number) li.get(0)).doubleValue();
            }

        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }


}